package com.example.itshome.network;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.itshome.activity.MainPageActivity;
import com.example.itshome.manager.UserInfoManager;
import com.example.itshome.util.SharePreferencesUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketTimeoutException;


public class LoginRequest extends AbstractNetworkRequest{

    private String username;
    private String password;
    private Toast toast;
    private View clicker;

    public LoginRequest setClicker(View clicker) {
        this.clicker = clicker;
        return this;
    }

    public LoginRequest setUsername(String username) {
        this.username = username;
        return this;
    }

    public LoginRequest setPassword(String password) {
        this.password = password;
        return this;
    }

    public LoginRequest setToast(Toast toast) {
        this.toast = toast;
        return this;
    }

    public LoginRequest(Activity activity) {
        super(activity,POST_METHOD);
    }

    @Override
    protected String getPathAndParams() {
        return "user/login?username="+username+"&password="+password;
    }

    @Override
    protected void handleResult(String result) {
        JSONObject resultJson=null;
        int code=0;
        try {
            resultJson=new JSONObject(result);
            code= resultJson.getInt("code");
            if(code==200){
                resultJson=resultJson.getJSONObject("data");
                String tokenRequest=resultJson.getString("toAuthentication");
                String tokenRefresh=resultJson.getString("toRefresh");

                UserInfoManager.getInstance().setTokenRequest(tokenRequest);
                UserInfoManager.getInstance().setTokenRefresh(tokenRefresh);

                SharePreferencesUtil util=new SharePreferencesUtil(activity,SharePreferencesUtil.USERFile);
                util.putString("tokenRequest",tokenRequest);
                util.putString("tokenRefresh",tokenRefresh);
                util.putString("username",username);
                setHandleResultRunnable(new Runnable() {
                    @Override
                    public void run() {
                        UserInfoManager.getInstance().getUser().setUsername(username);
                        Intent intent=new Intent(activity, MainPageActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                        activity.startActivity(intent);
                    }
                });
            }else if(code==500){
                setHandleResultRunnable(new Runnable() {
                    @Override
                    public void run() {
                        toast.cancel();
                        toast.setText("用户名或密码错误");
                        toast.setDuration(Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });
            }else {
                setHandleResultRunnable(new Runnable() {
                    @Override
                    public void run() {
                        toast.cancel();
                        toast.setText("登录失败");
                        toast.setDuration(Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });
            }

        }catch (JSONException e){
            e.printStackTrace();
        }
    }

    @Override
    protected void handleException(Exception exception) {
        setHandleExceptionRunnable(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
                toast.setDuration(Toast.LENGTH_SHORT);
                if(exception instanceof SocketTimeoutException){
                    toast.setText("登录超时");

                }else {
                    exception.printStackTrace();
                    toast.setText("未知异常");
                }
                toast.show();
            }
        });
    }

    @Override
    protected boolean canStart() {
        if(username.trim().length()>0&&password.trim().length()>0){
            clicker.setClickable(false);
            return true;
        }else {
            toast.cancel();
            toast.setText("请输入账号密码");
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.show();
            return false;
        }
    }

    @Override
    protected void afterResult() {
        clicker.setClickable(true);
    }
}
